<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<%@page import="fi.foyt.cs.persistence.domainmodel.oauth.AuthorizedClient"%>
<%@page import="fi.foyt.cs.controller.OAuthController"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.system.Property"%>
<%@page import="fi.foyt.cs.controller.SystemController"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.task.GeneralTask"%>
<%@page import="fi.foyt.cs.controller.TaskController"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.task.TaskType"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <jsp:include page="../fragments/head.jsp"></jsp:include>
  <script type="text/javascript">
    function genKey(lenght) {
	    var r = '';
	    for (var i = 0; i < lenght; i++) {
	      r += String.fromCharCode((Math.round(Math.random()) == 0 ? 65 : 97) + Math.round(Math.random() * 25));
	    }
	    return r;
	  }
    
    function generateClient(event) {
    	$('newClientId').value = genKey(Math.round(Math.random() * 5) + 10);
      $('newClientSecret').value = genKey(Math.round(Math.random() * 5) + 10);
      Event.stop(event);
    }
  </script>
</head>
<body>
	<jsp:include page="../fragments/header.jsp"></jsp:include>

	<%
	  OAuthController oAuthController = new OAuthController();
	  
	  if ("POST".equals(request.getMethod())) {
      int count = Integer.parseInt(request.getParameter("count"));

      for (int i = 0; i < count; i++) {
        String clientId = request.getParameter("client." + i + ".clientId");
        String clientSecret = request.getParameter("client." + i + ".clientSecret");
        String returnUrl = request.getParameter("client." + i + ".returnUrl");
        Boolean autoAcceptScopes = "1".equals(request.getParameter("client." + i + ".autoAcceptScopes"));
        String clientName = request.getParameter("client." + i + ".clientName");
        
        AuthorizedClient authorizedClient = oAuthController.findAuthorizedClientByClientIdAndClientSecret(clientId, clientSecret);
        oAuthController.updateAuthorizedClientReturnUrl(authorizedClient, returnUrl);
        oAuthController.updateAuthorizedClientAutoAcceptScopes(authorizedClient, autoAcceptScopes);
        oAuthController.updateAuthorizedClientClientName(authorizedClient, clientName);
      }
      
      String clientId = request.getParameter("client.-1.clientId");
      String clientSecret = request.getParameter("client.-1.clientSecret");
      String returnUrl = request.getParameter("client.-1.returnUrl");
      String clientName = request.getParameter("client.-1.clientName");
      Boolean autoAcceptScopes = "1".equals(request.getParameter("client.-1.autoAcceptScopes"));
      
      if ((clientId != null && !"".equals(clientId.trim())) && (clientSecret != null && !"".equals(clientSecret.trim()))) {
        oAuthController.createAuthorizedClient(clientId, clientSecret, returnUrl, clientName, autoAcceptScopes);
      }
	  }

	  pageContext.setAttribute("clients", oAuthController.listAuthorizedClients());
	%>

	<h1>Authorized clients</h1>

	<form method="POST">
		<input type="hidden" name="count" value="${fn:length(clients)}" />

		<table>
			<tr>
				<th>Client Id</th>
				<th>Client Secret</th>
        <th>Return Url</th>
        <th>Client Name</th>
        <th>Auto Accept Scopes</th>
			</tr>

			<c:set var="index" value="0" />

			<c:forEach items="${clients}" var="client">
				<tr>
					<td>${client.clientId}<input type="hidden" value="${client.clientId}" name="client.${index}.clientId" /></td>
          <td>${client.clientSecret}<input type="hidden" value="${client.clientSecret}" name="client.${index}.clientSecret" /></td>
          <td><input type="text" value="${client.returnUrl}" name="client.${index}.returnUrl" /></td>
          <td><input type="text" value="${client.clientName}" name="client.${index}.clientName" /></td>
          <td>
            <c:choose>
              <c:when test="${client.autoAcceptScopes}">
                <input type="checkbox" value="1" name="client.${index}.autoAcceptScopes" checked="checked"/>
              </c:when>
              <c:otherwise>
                <input type="checkbox" value="1" name="client.${index}.autoAcceptScopes" />
              </c:otherwise>
            </c:choose>
          </td>
          <td></td>
				</tr>

				<c:set var="index" value="${index + 1}" />
			</c:forEach>
			
			<tr>
        <td><input type="text" value="" name="client.-1.clientId" id="newClientId"/></td>
        <td><input type="text" value="" name="client.-1.clientSecret" id="newClientSecret"/></td>
        <td><input type="text" value="" name="client.-1.returnUrl" /></td>
        <td><input type="text" value="" name="client.-1.clientName" /></td>
        <td><input type="checkbox" value="1" name="client.-1.autoAcceptScopes" /></td>
        <td><button onclick="generateClient(event);">Generate</button></td>
      </tr>
		</table>
    <div style="margin-top: 16px;">
		  <input type="submit" value="Save" />
		</div>
	</form>

	<jsp:include page="../fragments/footer.jsp"></jsp:include>
</body>
</html>